/*
 * 
 * 
 * 
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.DepositDao;
import com.hboxs.asl.entity.Deposit;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Order;
import com.hboxs.asl.entity.Payment;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.util.Collections;

/**
 * Dao - 预存款
 */
@Repository("depositDaoImpl")
public class DepositDaoImpl extends BaseDaoImpl<Deposit, Long> implements DepositDao {

    public Page<Deposit> findPage(Member member, Pageable pageable) {
        if (member == null) {
            return new Page<Deposit>(Collections.<Deposit>emptyList(), 0, pageable);
        }
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Deposit> criteriaQuery = criteriaBuilder.createQuery(Deposit.class);
        Root<Deposit> root = criteriaQuery.from(Deposit.class);
        criteriaQuery.select(root);
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
        return super.findPage(criteriaQuery, pageable);
    }

    @Override
    public void generate(Deposit.Type type, BigDecimal credit, BigDecimal debit, String operator, String memo, Member member, Order order, Payment payment) {
        persist(new Deposit(type, credit, debit, member.getBalance(), operator, memo, member, order, payment));
    }

}